﻿#include "daemonlogdialog.h"
#include "ui_daemonlogdialog.h"

#include <QMessageBox>
#include <delegatecenter.h>

DaemonLogDialog::DaemonLogDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DaemonLogDialog)
{
    ui->setupUi(this);
//    daemonLogService = new DaemonLogService();
    nowPage = 1;
    pageCount = 20;

    ui->pageHelper->setPageCount(pageCount);
    connect(ui->pageHelper,&QPageHelper::signal_goto,this,&DaemonLogDialog::slot_gotoPage);

    tableModel = new QStandardItemModel();
    ui->tableView->setModel(tableModel);

    tableModel->setHorizontalHeaderItem(0,new QStandardItem(QString("id")));
    tableModel->setHorizontalHeaderItem(1,new QStandardItem(QString("时间")));
    tableModel->setHorizontalHeaderItem(2,new QStandardItem(QString("进程拉起类型")));
    tableModel->setHorizontalHeaderItem(3,new QStandardItem(QString("程序别名")));
    tableModel->setHorizontalHeaderItem(4,new QStandardItem(QString("进程路径")));
    tableModel->setHorizontalHeaderItem(5,new QStandardItem(QString("备注信息")));

    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);//单行选中
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);//设置单元格不可编辑

    QStringList enumList{"唤醒异常","首启守护唤醒", "常在守护唤醒", "CPU守护唤醒", "心跳守护唤醒","用户手动唤醒"};

    EnumDelegate * enumDelegate = new EnumDelegate(enumList, this);

    ui->tableView->setItemDelegateForColumn(2, enumDelegate);
}

DaemonLogDialog::~DaemonLogDialog()
{
    delete tableModel;
//    delete daemonLogService;
    delete ui;
}

void DaemonLogDialog::slot_gotoPage(qlonglong in_nowPage, int in_pageCount)
{
    nowPage = in_nowPage;
    pageCount = in_pageCount;
    slot_selectAll();
}

void DaemonLogDialog::slot_selectAll()
{
    tableModel->removeRows(0,tableModel->rowCount());
    uint64_t total;
    QList<DaemonLog> list;
    QString progressName = ui->name->text();
    if(daemonLogService.getPageList(total,list,nowPage,pageCount,progressName)){
        for (int i=0;i<list.size();i++) {
            tableModel->insertRow(tableModel->rowCount());
            int inserRow = tableModel->rowCount() -1;
            tableModel->setItem(inserRow,0,new QStandardItem(QString::number(list.at(i).getId())));
            tableModel->setItem(inserRow,1,new QStandardItem(QString(list.at(i).getStart_time())));
            tableModel->setItem(inserRow,2,new QStandardItem(QString::number(list.at(i).getStart_type())));
            tableModel->setItem(inserRow,3,new QStandardItem(QString(list.at(i).getProgress_name())));
            tableModel->setItem(inserRow,4,new QStandardItem(QString(list.at(i).getProgress_path())));
            tableModel->setItem(inserRow,5,new QStandardItem(QString(list.at(i).getRemark())));
        }
        ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
        ui->tableView->resizeRowToContents(0);
        ui->pageHelper->setItemTotal(total);
//        ui->pageHelper->setPageCount(pageCount);
        ui->pageHelper->setCurrentPage(nowPage);
    }else{
        QMessageBox::critical(this,"警告","数据库连接异常！");
    }
}

void DaemonLogDialog::on_selBtn_clicked()
{
    nowPage = 1;
    slot_selectAll();
}

void DaemonLogDialog::on_clearLog_clicked()
{
    if(daemonLogService.deleteAll()){
        nowPage = 1;
        slot_selectAll();
    }else{
        QMessageBox::critical(this,"警告","数据库连接异常！");
    }
}
